import React from 'react';
import {Form, Row, Col, Button, DatePicker, message ,Divider,Select,Input} from 'antd';
import StatisticsBox from '../../components/box/statistics';
import {sessionManager} from '../../utils/sessionStorage';
import AdjustHis from './AdjustHis';
import {injectIntl} from 'react-intl';
import Grid from '../../components/table/grid';
import axios from 'axios';
import moment from 'moment';
import { getYear } from '../../utils/Array';
import qs from 'qs';
const { RangePicker } = DatePicker;
const FormItem = Form.Item;
const createForm = Form.create;
const Option = Select.Option;
class discountDetail extends React.Component {
	constructor(props) {
		super(props);
		this.common = this.props.intl.messages.common;
		this.columns = [
			{
				title: '活动名称',
				dataIndex:'activityName',
				key: 'activityName'
			},
			{
				title: '使用时间',
				dataIndex: 'useTime',
				key: 'useTime',
			},
			{
				title: '营销类型',
				dataIndex: 'type',
				key: 'type',
				render: (text) => {
					return <span>{text.toString() === '1' ? '抵用券': '折扣费用'}</span>
				}
			},
			{
				title: '金额',
				dataIndex: 'fee',
				key: 'fee',

			}
		];
		this.state = {
			params:{
			},
			summary: [],
			visible: false,
			refresh:0
		}
	}

	componentWillMount() {
		this.getBillTotal()
	}

	getBillTotal(values){
		let newParams={
			// useStartTime:'',
			// useEndTime:'',
			ym:sessionManager.get('discountInfo').ym,
			settlementUserId:sessionManager.get('discountInfo').settlementUserId
		}
		// if(values){
		// 	newParams={
		// 				useStartTime:values.startEndTime[0]._i,
		// 				useEndTime:values.startEndTime[1]._i,
		// 				ym:sessionManager.get('discountInfo').ym,
		// 				settlementUserId:sessionManager.get('discountInfo').settlementUserId,
		// 				refresh:this.state.refresh+1
		// 			}
		// }
		this.setState({
			params:newParams
		});
	}


	handleSubmit = (e) => {
		e.preventDefault();
		this.props.form.validateFieldsAndScroll((err, values) => {
			if (!!err) {
				return;
			}
			console.log('values----------',values)
			// this.getBillTotal(values)
			this.setState({
				params:{
					useStartTime:moment(values.startEndTime[0]&&values.startEndTime[0]._d).format('YYYY.MM.DD'),
					useEndTime:moment(values.startEndTime[1]&&values.startEndTime[1]._d).format('YYYY.MM.DD'),
					ym:sessionManager.get('discountInfo').ym,
					settlementUserId:sessionManager.get('discountInfo').settlementUserId,
				},
				refresh:this.state.refresh+1
			})
			// this.setState(Object.assign(this.state.params,values));
			// this.setState({
			// 	refresh:this.state.refresh+1
			// })
		});
	};

	render() {
		const token=(JSON.parse(sessionStorage.getItem("token"))||{}).token;
		let locale=sessionManager.get('locale');
		let exportQs=`${qs.stringify(this.state.params)}&token=${token}&locale=${locale}`;
		console.log('exportQs',exportQs);
		const {getFieldDecorator} = this.props.form;
		const dateFormat = 'YYYY.MM.DD';
		return (
			<div>
				<form  className="SC-form">
					<Row gutter={24} type="flex">
						<Col xl={6} xxl={4}>
							<FormItem>
								{getFieldDecorator('startEndTime',
								)(
									<RangePicker format={dateFormat} />
								)}
							</FormItem>
						</Col>
						<Col>
							<FormItem>
								<Button type="primary" htmlType="submit"
										onClick={this.handleSubmit} className="ant-btn-84">{this.common.search}</Button>
								<a href={`${axios.defaults.baseURL}/api/billSplit/marketingCost/export?${exportQs}`}>
									<Button type="primary">{this.common.exportStatement}</Button>
								</a>
							</FormItem>
						</Col>
					</Row>
				</form>
				<Grid url="api/settlement/billSplit/marketingCost" columns={this.columns} refresh={this.state.refresh}  params={this.state.params}/>
			</div>
		)
	}
}

discountDetail = createForm()(discountDetail);
export default injectIntl(discountDetail)
